package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealGroup;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import com.itheima.service.SetmealGroupService;
import com.itheima.service.SetmealService;
import com.itheima.utils.FileUtils;
import com.itheima.vo.CheckGroupVo;
import com.itheima.vo.SetmealVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * company: www.abc.com
 * Author: 29746
 * Create Data: 2020/11/13
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {


    @Resource
    private SetmealGroupService setmealGroupService;

    @Resource
    private CheckGroupService checkGroupService;

    @Resource
    private CheckItemService checkItemService;

    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public List<Setmeal> findAll() {
        //查询有效数据没有被逻辑删除的数据
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag",0);
        return this.list(queryWrapper);
    }

    @Override
    public void saveSetmealAndGroup(SetmealDto setmealDto) {

        if(setmealDto.getSetmeal().getImg()!=null){
           // RedisUtil.removeSetMember("file:allset",setmealDto.getSetmeal().getImg());
         redisTemplate.opsForSet().remove("file:allset",setmealDto.getSetmeal().getImg());
        }
        //1.保存套餐信息
        this.saveOrUpdate(setmealDto.getSetmeal());
        //2.删除中间表信息,根据套餐id
        setmealGroupService.remove(new QueryWrapper<SetmealGroup>().eq("setmeal_id",setmealDto.getSetmeal().getId()));
        //3.循环往中间表插入信息
        ArrayList<SetmealGroup> list = new ArrayList<>();
        for (Long groupId : setmealDto.getGroupIds()) {
            SetmealGroup setmealGroup = new SetmealGroup();
            setmealGroup.setSetmealId(setmealDto.getSetmeal().getId());
            setmealGroup.setGroupId(groupId);
            list.add(setmealGroup);
        }
        setmealGroupService.saveBatch(list);
    }

    @Override
    public boolean deleteById(Long id) {
        //删除时不仅删除套餐表，还要删除中间表的信息
        this.removeById(id);
        setmealGroupService.remove(new QueryWrapper<SetmealGroup>().eq("setmeal_id",id));
        return true;
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Page<Setmeal> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag",0);
        if (queryPageBean.getQueryString()!=null && "".equals(queryPageBean.getQueryString().trim())==false){
            queryWrapper.and(wrapper->{
                wrapper.like("code",queryPageBean.getQueryString())
                        .or().like("name",queryPageBean.getQueryString())
                        .or().like("help_code",queryPageBean.getQueryString());
            });
        }
        this.page(page,queryWrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public void clearOssObject() {
        Set<Object> allSet=redisTemplate.opsForSet().members("file:allset");
        if (allSet!=null){
            for (Object path:allSet){
                String fileKey="file:"+path;
                if (redisTemplate.hasKey(fileKey)==false){
                    FileUtils.delete((String) path);
                    redisTemplate.opsForSet().remove("file:allset",path);
                }
            }
        }

    }

    @Override
    public SetmealVo findSetmealVo(Long setmealId) {
        //1.查询套餐BySetMealId
        SetmealVo setmealVo = this.baseMapper.findSetMealVoBySetMealId(setmealId);

        //2.查询检查组BySetMealId,放到setmealVo中
        List<CheckGroupVo> checkGroupVoList= checkGroupService.findCheckGroupBySetMealId(setmealId);
        //3.遍历检查组，把检查项集合放到CheckGroupVo的List<CheckItem> checkItem属性中
        for (CheckGroupVo checkGroupVo : checkGroupVoList) {
            //4.查询检查项ByGroupId
            List<CheckItem> checkItemList=checkItemService.findCheckItemByGroupId(checkGroupVo.getId());
            checkGroupVo.setCheckItemList(checkItemList);
        }
        setmealVo.setCheckGroupVoList(checkGroupVoList);
        return setmealVo;
    }
}
